<template >
  <el-dialog
    :title="toggleTitle"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="400px" >
    <el-form
      ref="dataForm"
      :model="dataForm"
      :rules="rules"
      label-width="100px" >
      <el-form-item label="角色组名称" prop="name" >
        <el-input placeholder="角色组名称" v-model="dataForm.name" ></el-input >
      </el-form-item >
    </el-form >
    <div slot="footer" class="dialog-footer" >
      <el-button @click="close" >取消</el-button >
      <el-button type="primary" @click="submitForm" >确定</el-button >
    </div >
  </el-dialog >
</template >

<script >
  // api
  import { getRoleGroupDetail, updateRolesGroups, addRolesGroups } from '@/api/roles'

  export default {
    name: 'groups-add-or-update',
    data() {
      return {
        visible: false,
        dataForm: {
          id: '',
          routerIds: '',
          name: ''
        },
        rules: {
          name: [
            { required: true, message: '角色组名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    computed: {
      toggleTitle() {
        return !this.dataForm.id ? '新增' : '编辑'
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || ''
        this.visible = true
        if (this.dataForm.id) {
          getRoleGroupDetail({ id: this.dataForm.id }).then((res) => {
            this.dataForm.name = res.data.data.name
          })
        } else {
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
          })
        }
      },
      submitForm() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (!this.dataForm.id) {
              addRolesGroups(this.dataForm).then(() => {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                })
                this.visible = false
                this.$emit('refresh')
              })
            } else {
              updateRolesGroups(this.dataForm).then(() => {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })

                this.visible = false
                this.$emit('refresh')
              })
            }
          } else {
            return false
          }
        })
      },
      close() {
        this.visible = false
        this.$refs['dataForm'].resetFields()
      }
    }
  }
</script >


<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" rel="stylesheet/scss" type="text/scss" >

</style >
